/**
 * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef OHOS_NAPI_UTIL_H
#define OHOS_NAPI_UTIL_H

#include "napi/native_api.h"
#include "napi_tool.h"
#include "types.h"
#include <multimedia/image_framework/image_pixel_map_mdk.h>
#include <string>
#include <uv.h>

struct AsyncCallbackError {
        int32_t code{0};
        std::string message;
        std::string name;
        std::string stack;
};

class CallbackListener {
public:
        CallbackListener(napi_env env, napi_ref ref) : env_(env), callbackRef_(ref) {}
        ~CallbackListener() = default;
        void OnCallback() const;
        void OnCallbackAsync() const;
        PixelMap pixelMap;
        AsyncCallbackError error;

private:
        napi_env env_;
        napi_ref callbackRef_ = nullptr;
};

struct CallbackInfo {
        const CallbackListener *listener_;
        CallbackInfo(const CallbackListener *listener) : listener_(listener) {}
};

bool IsMatchType(const napi_env &env, const napi_value &value,
                 const napi_valuetype &type);

bool GetStringValue(const napi_env &env, const napi_value &value,
                    std::string &result);

std::vector<double> GetLevelsArray(const napi_env &env,
                                   const napi_value &value);

napi_value CreateBusinessError(const napi_env &env, const int32_t errCode,
                               const std::string &errMessage);

std::string GetNapiError(int32_t errorCode);

void ThrowErr(const napi_env &env, const int32_t errCode,
              const std::string &printMsg);

void CreatePixelMap(const napi_env &env, const PixelMap &pixelMap,
                    napi_value &result);

#endif // OHOS_NAPI_UTIL_H
